package com.onlineshop.highgee.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import com.onlineshop.highgee.dao.UserDao;
import com.onlineshop.highgee.dao.UserInfoDao;
import com.onlineshop.highgee.daoimpl.UserDaoImpl;
import com.onlineshop.highgee.daoimpl.UserDaoInfoImpl;
import com.onlineshop.highgee.entity.User;
import com.onlineshop.highgee.entity.UserInfo;

/**
 * Servlet implementation class User
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			System.out.println("UserServlet_doPost");
			// 获取数据（页面）
			String user_name = request.getParameter("user_name");
			String password = request.getParameter("password");	
			User user = new User();
			UserInfoDao userInfoDao = new UserDaoInfoImpl();
			UserDao userDao = new UserDaoImpl();
			UserInfo userInfo = new UserInfo();

			user.setUserName(user_name);
			user.setPassword(password);
	
			HttpSession session = request.getSession();
			session.setAttribute("user_name", user_name);
			userInfoDao.addUserInfo(userInfo);
			// userDao.addUser(user);
			userDao.findByNameAndPassword(user_name, password);
			//
			request.getRequestDispatcher("index.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
